//
//  SportsTableViewController.m
//  B
//
//  Created by lanou on 16/5/17.
//  Copyright © 2016年 裴启航. All rights reserved.
//

#import "SportsTableViewController.h"
#import "SportsModel.h"
#import "SportsTableViewCell.h"
#import "WebviewViewController.h"

@interface SportsTableViewController ()
@property (nonatomic, strong) NSMutableArray *listArrs;
@end

@implementation SportsTableViewController

- (NSMutableArray *)listArrs
{
    if (!_listArrs) {
        _listArrs = [NSMutableArray array];
    }
    return _listArrs;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.navigationItem.title = @"运动知识";
    [self.tableView registerNib:[UINib nibWithNibName:@"SportsTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    [self requestData];
}

- (void)requestData
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:@"http://fit.jianmeile.cn:8001/study/articles?apiKey=dff83d803c8fcea96b7122785182ac24&apiSign=fac733b3a811d5f312b5ab10aaf7b457&firstId=3&secondId=8&t=1463487837&thirdId=8" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *array = responseObject[@"data"];
        for (NSDictionary *dic in array) {
            SportsModel *mode = [[SportsModel alloc] init];
            [mode setValuesForKeysWithDictionary:dic];
            [self.listArrs addObject:mode];
        }
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.listArrs.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 225;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SportsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    SportsModel *model = self.listArrs[indexPath.row];
    cell.nameLable.text = model.title;
    cell.contentLable.text = model.excerpt;
    [cell.imageView1 sd_setImageWithURL:[NSURL URLWithString:model.background]];
    // Configure the cell...
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WebviewViewController *detail = [[WebviewViewController alloc] init];
    SportsModel *model = self.listArrs[indexPath.row];
    detail.string = model.contentURL;
    detail.title = model.title;
    [self.navigationController pushViewController:detail animated:YES];
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
